package com.example.bledemo.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;

import java.util.ArrayList;

/**
 * 自定义地图控件，支持绿色虚线箭头路径
 */
public class MyBaseMapAndLines extends androidx.appcompat.widget.AppCompatImageView {

    // 线坐标
    public ArrayList<MapLineCoord> mapLineCoords;

    public MyBaseMapAndLines(Context context) {
        super(context);
        mapLineCoords = new ArrayList<>();
    }

    public MyBaseMapAndLines(Context context, AttributeSet attrs) {
        super(context, attrs);
        mapLineCoords = new ArrayList<>();
    }

    public ArrayList<MapLineCoord> getMapLineCoords() {
        return mapLineCoords;
    }

    public int getLineSize() {
        return mapLineCoords.size();
    }

    public void clearLines() {
        mapLineCoords.clear();
        invalidate();
    }

    public void addLines(ArrayList<MapLineCoord> mapLineCoords) {
        this.mapLineCoords.addAll(mapLineCoords);
        invalidate();
    }

    public MapLineCoord getLine(int index) {
        return mapLineCoords.get(index);
    }

    public void addLine(MapLineCoord mapLineCoord) {
        mapLineCoords.add(mapLineCoord);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mapLineCoords == null || mapLineCoords.size() <= 0)
            return;
        
        // 创建路径对象，用于绘制平滑的路线
        Path path = new Path();
        path.moveTo(mapLineCoords.get(0).getViewX(), mapLineCoords.get(0).getViewY());
        
        // 添加所有路径点
        for (int i = 1; i < mapLineCoords.size(); i++) {
            path.lineTo(mapLineCoords.get(i).getViewX(), mapLineCoords.get(i).getViewY());
        }
        
        // 创建绿色虚线画笔
        Paint linePaint = new Paint();
        linePaint.setAntiAlias(true);  // 抗锯齿效果
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(6.0f);  // 线宽
        linePaint.setStrokeCap(Paint.Cap.ROUND);  // 圆形线帽
        linePaint.setStrokeJoin(Paint.Join.ROUND);  // 圆形连接点
        linePaint.setColor(Color.parseColor("#4CAF50"));  // 绿色
        
        // 设置虚线效果和路径平滑效果
        DashPathEffect dashPathEffect = new DashPathEffect(new float[]{15, 10}, 0);
        CornerPathEffect cornerPathEffect = new CornerPathEffect(20.0f);
        // 组合两种效果
        linePaint.setPathEffect(new android.graphics.PathEffect() {
            @Override
            public boolean equals(Object obj) {
                return super.equals(obj);
            }
        });
        // 暂时只使用虚线效果，避免冲突
        linePaint.setPathEffect(dashPathEffect);
        
        // 绘制主路径
        canvas.drawPath(path, linePaint);
        
        // 绘制箭头
        drawArrows(canvas, path);
        
        // 添加调试信息
        android.util.Log.d("MyBaseMapAndLines", "绘制完成，路径点数量: " + mapLineCoords.size());
    }
    
    /**
     * 在路径上绘制箭头
     */
    private void drawArrows(Canvas canvas, Path path) {
        if (mapLineCoords.size() < 2) return;
        
        Paint arrowPaint = new Paint();
        arrowPaint.setAntiAlias(true);
        arrowPaint.setStyle(Paint.Style.FILL);
        arrowPaint.setColor(Color.GREEN);  // 改为红色，更容易看到
        
        PathMeasure pathMeasure = new PathMeasure(path, false);
        float pathLength = pathMeasure.getLength();
        
        // 添加调试信息
        android.util.Log.d("MyBaseMapAndLines", "路径长度: " + pathLength + ", 路径点数量: " + mapLineCoords.size());
        
        // 如果路径太短，直接在每个路径点上绘制箭头
        if (pathLength < 100) {
            for (int i = 0; i < mapLineCoords.size(); i++) {
                float x = mapLineCoords.get(i).getViewX();
                float y = mapLineCoords.get(i).getViewY();
                // 绘制一个简单的圆形作为箭头
                canvas.drawCircle(x, y, 8, arrowPaint);
                android.util.Log.d("MyBaseMapAndLines", "绘制圆形箭头: (" + x + ", " + y + ")");
            }
            return;
        }
        
        // 在路径上每隔一定距离绘制箭头
        float arrowSpacing = 40.0f;  // 减少箭头间距
        float arrowSize = 10.0f;     // 增大箭头大小
        
        for (float distance = arrowSpacing; distance < pathLength - arrowSpacing; distance += arrowSpacing) {
            float[] pos = new float[2];
            float[] tan = new float[2];
            
            if (pathMeasure.getPosTan(distance, pos, tan)) {
                float angle = (float) Math.atan2(tan[1], tan[0]);
                drawArrow(canvas, pos[0], pos[1], angle, arrowSize, arrowPaint);
                // 添加调试信息
                android.util.Log.d("MyBaseMapAndLines", "绘制箭头: (" + pos[0] + ", " + pos[1] + ") 角度: " + angle);
            }
        }
    }
    
    /**
     * 绘制单个箭头
     */
    private void drawArrow(Canvas canvas, float x, float y, float angle, float size, Paint paint) {
        canvas.save();
        canvas.translate(x, y);
        canvas.rotate((float) Math.toDegrees(angle));
        
        Path arrowPath = new Path();
        // 创建更明显的箭头形状
        arrowPath.moveTo(-size, -size/2);
        arrowPath.lineTo(0, size);
        arrowPath.lineTo(size, -size/2);
        arrowPath.close();
        
        // 绘制箭头填充
        canvas.drawPath(arrowPath, paint);
        
        // 绘制箭头边框，使其更明显
        Paint borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(2.0f);
        borderPaint.setColor(Color.WHITE);
        canvas.drawPath(arrowPath, borderPaint);
        
        canvas.restore();
    }

    /**
     * 地图线拐点坐标
     */
    public class MapLineCoord {
        private float firstX;
        private float firstY;
        private float viewX;
        private float viewY;

        public MapLineCoord() {
        }

        public MapLineCoord(float firstX, float firstY, float viewX, float viewY) {
            this.firstX = firstX;
            this.firstY = firstY;
            this.viewX = viewX;
            this.viewY = viewY;
        }

        public float getFirstX() {
            return firstX;
        }

        public void setFirstX(float firstX) {
            this.firstX = firstX;
        }

        public float getFirstY() {
            return firstY;
        }

        public void setFirstY(float firstY) {
            this.firstY = firstY;
        }

        public float getViewX() {
            return viewX;
        }

        public void setViewX(float viewX) {
            this.viewX = viewX;
        }

        public float getViewY() {
            return viewY;
        }

        public void setViewY(float viewY) {
            this.viewY = viewY;
        }

        @Override
        public String toString() {
            return "MapLineCoord{" +
                    "firstX=" + firstX +
                    ", firstY=" + firstY +
                    ", viewX=" + viewX +
                    ", viewY=" + viewY +
                    '}';
        }
    }
} 